<!--<link rel="stylesheet" type="text/css" href="css/page.css"/>-->
<link rel="stylesheet" href="http://www.12301.cc/css/common/www.12301.cc.global.css"/>
<script type="text/javascript" src="http://www.12301.cc/js/global/www.12301.cc.global.js"></script>
<style type="text/css">
	button{cursor: pointer;}
	.wrap{width:80%;float:right;}
	table{width:100%; line-height:23px;margin:10px 0; border-color:#d5dce2;}
	th,td{ text-align:center; border-color:#d5dce2; line-height:30px;}
	th{background:#f2f9ff;}
	.pri{width:109px;height:22px;text-align:center;}
	.fenyeiWrap{width:85%;}
	#tplid{display:none;}
	.addcp {background:url("http://www.12301.cc/images/btn23_two1.png") no-repeat scroll left top;display: inline-block;height: 23px;padding-left: 8px;text-decoration: none;}
	.addcp span {background:url("http://www.12301.cc/images/btn23_two1.png") no-repeat scroll right top;color: #FFFFFF;display: inline-block;font-size: 12px;font-weight: bold;height: 23px;line-height: 23px;padding-right: 8px;}
	.ty{background:#f0851d; border:1px solid #a9550f; height:25px;width:75px; color:#fff; padding-bottom:2px;  padding-bottom:0px\9; }
	.tpllist{float:right;line-height: 25px;}
	.txtlf{ text-align:left; padding-left:15px;}
	.add,.rude {display: block;width: 10px;height: 11px;border: 1px solid;line-height: 9px;
	line-height: 8px\9;padding-right: 1px;font-size: 15px;cursor: pointer;}
	.add {position: absolute;right: -20px;top: 9px;}
	.rude{position: absolute;right: 45px;top: 9px;}
</style>
<script type="text/javascript">
	function getTpls() {
        $.getJSON("call/hasTpl.php", function(tpl) {
            if (tpl) {
                $("#tplid").show();
                var now = $("#now").val();
                for(var i = 0; i < tpl.length; i++){
                    $("#tplid").append(
                    	"<option "+(tpl[i]['id']==now?"selected":"")+" value='"+tpl[i]['id']+"'>"+tpl[i]['title']+"</option>"
                    );
                }
            } else {
                if (confirm('检测到您尚未创建过模板,是否创建?')) {
                    window.location = 'addtpl.html';
                }
            }
        })
	};
	getTpls();

	function setPriceRequest() {
		$.post("priceset_new.php", $("#priceset_form").serialize(), function(result) {
			if (result.status == 0) {
				PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+result.info+'</p>');
			} else if (result.status == 1) {
				PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+result.info+'</p>');
				setTimeout(function () { 
					location.reload();
				}, 1000);
			} else if (result.status == 2) {
				window.location.href = 'dlogin_n.html';
			}
		}, "json")
	}

	function useTpl(){
		if ($("#tplid").css("display") == "none") {
			getTpls();
		} else {
			if ($("#tplid").val()) {
				$("#bind").val(1);
			}
			setPriceRequest();
		}
	}

	function selectTpl(){
		var t = $('#tplid').val();
		if (t > 0) {
			$("#bind").val(0);
			$.getJSON("call/getTpl.php", {tplid:t}, function(tpl) {
				$(":checkbox").each(function() {
					var ckbox = $(this);
					if (ckbox.val()!="all") {
						if (!isNaN(tpl[ckbox.val()])) {
							ckbox.attr("checked",true).parent().parent().find(":text").attr("disabled",false).val(($("#stype").length && $("#stype").attr("checked") ? 0 :Number(ckbox.parent().parent().children().eq(2).html()))+Number(tpl[ckbox.val()]));
						} else {
							ckbox.attr("checked",false).parent().parent().find(":text").attr("disabled",true);
						}
					}
				});
			});
		} else if (t == -1) {
			window.location='addtpl.html';
		}
	}

	function savePrice(){
		$("#bind").val(0);
		if ($("#tplid").val()) {
			if(confirm('是否将该分销商与所选模板绑定,绑定后可通过修改模板修改该分销商的配置?')) {
				$("#bind").val(1);
			}
		}
		setPriceRequest();
	}

	function changePrice(object) {
		var action = 0;
		if ($(object).attr("class")=="add") {
			action = 1;
		}
		$(".pri").each(function(i,ele) {
			if($(this).attr("disabled")!="disabled"){
				var price = $(this).val();
				var supply = Number($(this).parents().find(".supply").html());
				if (price == '') price = supply;
				if (action == 1) {
					price = Number(price) + 1;
				} else {
					price = Number(price) - 1;
					if (price < supply) price = supply;
				}
				price = price.toFixed(2);
				$(this).val(price);
			}
		});
	}

	function selectAll() {
        $("#listTable").find(".tids").each(function(index,inp) {
            $(inp).prop("checked", "checked");
			$(this).parent().parent().find(':text').attr('disabled', !this.checked);
        })
    };

    function cancelAll() {
        $("#listTable").find(".tids").each(function(index,inp) {
            $(inp).prop("checked", "");
			$(this).parent().parent().find(':text').attr('disabled', !this.checked);
        })
    };

	$(document).ready(function(){
		//页面点击量统计
		page_hit_request('自供应产品价格配置', 'priceset.html');

	    function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]); return null;
		}
		$(".addcp").attr("did",getQueryString("?"))
	    //全选
	    $("#selectAllBtn").on("click",function(e){
	        selectAll();
	    })
	    //全不选
	    $("#cannelAllBtn").on("click",function(e){
	        cancelAll();
	    })
		//搜索
		$("#btn").click(function(){
			var did=$("#did").val();
			var prodname=$("#prodname").val();
			window.location="priceset.html?did="+did+"&prodname="+prodname;
		})
	});
</script>
<div class="wrap">
<h3 class="mc_title">
	<span class="mctit_1"><span class="mctit_2 ">自供应产品</span></span>
	<span class="mctit_1 mctit_3"><a href="priceset_sale.html?did=<?=$did?>" class="mctit_2 mctit_4">转分销产品</a></span>
</h3>
</br>
<iframe name="ifr" style="display:none;"></iframe>
  	<form action="priceset.php" id="priceset_form" target="ifr" method="post">
		<span style="font-size:14px;font-weight:bold;color:#2A98DA;margin-right:10px;vertical-align: -2px;"><?=$dname?></span>
		<select name="tplid" id="tplid" onchange="selectTpl()">
			<option value="">-请选择模板-</option>
			<option value="-1">--新增模板--</option>
		</select>
		<button type="button" onclick="useTpl()" class="ty">套用模板</button>
		<font color="red">您可以选择套用模板来快速配置结算价。</font>
		<span style="font-size:14px;color:#2A98DA;">产品搜索: <input type="test" name="prodname" id="prodname"></span>
		<input type="button" id="btn" style="width:60px" value="搜索">
		<a class="tpllist" onclick="history.go(-1)" href="javascript:void(0);">返回>></a>
		<input type="hidden" name="did" id="did" value="<?=$did?>" />
		<input type="hidden" name="bind" id="bind" value="<?=($tplid?1:0)?>" />
		<input type="hidden" id="now" value="<?=$tplid?>" />
		<table border="1" id="listTable">
			<tr>
				<!-- <th>购买权限</th> -->
				 <th>
					<a id="selectAllBtn" class="ckall" href="javascript:;">全选</a>
					<a id="cannelAllBtn" class="reck" href="javascript:;">取消</a>
				</th>
				<th>产品名称</th> 
				<th>供货价</th>
				<th>
					<div style="position:relative; display:inline-block;" >
						<span onclick="changePrice(this);" class="rude">-</span>
						<span class="p_t">结算价</span>
						<span onclick="changePrice(this);" class="add">+</span>
					</div>
				</th>
			</tr>
		<?	foreach($prod as $p):?>
			<tr>
				<td><input type="checkbox" name="pid[]" class="tids check_" ty="<?=$p['checked']?>" data="<?=$p['id']?>" id="" <?=$p['checked']?> value="<?=$p['id']?>" onchange="$(this).parent().parent().find(':text').attr('disabled',!this.checked);"/></td>
				<td class="txtlf"><?=$p['p_name']?></td>
				<td><?=$p['fp']?></td>
				<td><input class="pri" type="text" <?=$p['disabled']?> name="pri[<?=$p['id']?>]" value="<?=$p['pri']+$p['fp']?>" /></td>
			</tr>
		<?	endforeach;?>
			
		</table>
		<?//page($total,$currentPage,$pageSize);?>
		<a class="addcp" onclick="savePrice()" did="" href="javascript:void(0);"><span>保存价格</span></a>
  </form>
</div>
<script>
$(".check_").live("click",function(e){ 
	var target = $(e.currentTarget);
	var did = $(".addcp").attr("did");
	var pid = target.attr("data");
	if (target.attr("checked") == undefined) {
		if (target.attr("ty") == "checked") {
		  var right = confirm("所有分销商不再分销此产品 ？ “确定”不再分销，“取消”放弃操作")
		  if (right == true) {
				var typeData={pid:pid, did:did};
				$.ajax({
					type:'POST',
					url: 'priceset_dt.php',
					data: typeData, 
					dataType:'json',
				}).done(function(res) {
					if (res.status == "success") {
						PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+res.msg+'</p>');
						setTimeout(function () { 
							location.reload();
						}, 1000);
					} else {
						PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
					}
				})
			} else {
			   return false;
			}
		}
	}
})
</script>